package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysFlowDefinition;
import com.ysd.lis.entity.SysFlowNode;
import com.ysd.lis.entity.SysFlowNodeApprover;
import com.ysd.lis.entity.SysFlowNodeEvent;
import com.ysd.lis.mapper.sys.SysFlowDefinitionMapper;
import com.ysd.lis.mapper.sys.SysFlowNodeApproverMapper;
import com.ysd.lis.mapper.sys.SysFlowNodeEventMapper;
import com.ysd.lis.mapper.sys.SysFlowNodeMapper;
import com.ysd.lis.request.SysFlowParam;
import com.ysd.lis.service.SysFlowDefinitionService;
import com.ysd.lis.service.SysFlowNodeEventService;
import com.ysd.lis.service.SysFlowNodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-04-01
 */
@Service
@Slf4j
public class SysFlowDefinitionServiceImpl extends ServiceImpl<SysFlowDefinitionMapper, SysFlowDefinition> implements SysFlowDefinitionService {

    @Autowired
    SysFlowDefinitionMapper sysFlowDefinitionMapper;
    @Autowired
    SysFlowNodeService sysFlowNodeService;
    @Autowired
    SysFlowNodeEventService sysFlowNodeEventService;
    @Autowired
    SysFlowNodeMapper sysFlowNodeMapper;
    @Autowired
    SysFlowNodeEventMapper sysFlowNodeEventMapper;
    @Autowired
    SysFlowNodeApproverMapper sysFlowNodeApproverMapper;

    @Override
    public Result findDefinitionList(SysFlowParam param) {
        LambdaQueryWrapper<SysFlowDefinition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysFlowDefinition::getDelFlag,0);
        if (StringUtils.isNotBlank(param.getSeachValue())) {
            queryWrapper.and(wrapper -> wrapper.like(SysFlowDefinition::getName, param.getSeachValue())
                    .or().like(SysFlowDefinition::getCode, param.getSeachValue()));
        }
        queryWrapper.orderByAsc(SysFlowDefinition::getSeq);
        List<SysFlowDefinition> definitionList = sysFlowDefinitionMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", definitionList);

    }

    @Override
    public Result addDefinition(SysFlowDefinition definition) {
        int insert = sysFlowDefinitionMapper.insert(definition);
        if(insert>0){
            //同时生成开始节点，审批节点，结束节点
            List<SysFlowNode> flowNodeList = new ArrayList<>();

            SysFlowNode beginFlowNode = new SysFlowNode();
            beginFlowNode.setName("发起");
            beginFlowNode.setCode("begin");
            beginFlowNode.setDefinitionId(definition.getId());
            beginFlowNode.setActivityType("startEvent");
            beginFlowNode.setSeq(1);
            flowNodeList.add(beginFlowNode);

            SysFlowNode spFlowNode = new SysFlowNode();
            spFlowNode.setName("普通审批");
            spFlowNode.setCode("");
            spFlowNode.setDefinitionId(definition.getId());
            spFlowNode.setActivityType("flowEvent");
            spFlowNode.setSeq(2);
            flowNodeList.add(spFlowNode);

            SysFlowNode endFlowNode = new SysFlowNode();
            endFlowNode.setName("结束");
            endFlowNode.setCode("end");
            endFlowNode.setDefinitionId(definition.getId());
            endFlowNode.setActivityType("endEvent");
            endFlowNode.setSeq(999);
            flowNodeList.add(endFlowNode);
            sysFlowNodeService.saveBatch(flowNodeList);

            List<SysFlowNodeEvent> nodeEventList = new ArrayList<>();
            //默认生成开始节点的下一步事件
            SysFlowNodeEvent beginEvent = new SysFlowNodeEvent();
            beginEvent.setName("提交");
            beginEvent.setDefinitionId(definition.getId());
            beginEvent.setNodeId(beginFlowNode.getId());
            beginEvent.setSeq(1);
            beginEvent.setNextNode(spFlowNode.getId());
            nodeEventList.add(beginEvent);

            SysFlowNodeEvent spEvent = new SysFlowNodeEvent();
            spEvent.setName("同意");
            spEvent.setDefinitionId(definition.getId());
            spEvent.setNodeId(spFlowNode.getId());
            spEvent.setSeq(1);
            spEvent.setNextNode(endFlowNode.getId());
            nodeEventList.add(spEvent);
            sysFlowNodeEventService.saveBatch(nodeEventList);


            return Result.succ(1, "新增成功", definition);
        }else {
            return Result.fail("新增失败");
        }
    }

    @Override
    public Result editDefinition(SysFlowDefinition definition) {
        int updateById = sysFlowDefinitionMapper.updateById(definition);
        if(updateById>0){
            return Result.succ(1,"修改成功","");
        }else{
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result deleteDefinition(SysFlowDefinition definition) {
        String id = definition.getId();
        int d = sysFlowDefinitionMapper.deleteById(id);
        if(d>0){
            //删除流程下的节点及节点审批人，节点事件
            //删除流程下的节点
            LambdaQueryWrapper<SysFlowNode> delNodeWrapper = new LambdaQueryWrapper<>();
            delNodeWrapper.eq(SysFlowNode::getDefinitionId,id);
            sysFlowNodeMapper.delete(delNodeWrapper);
            //删除节点下的审批人员信息
            LambdaQueryWrapper<SysFlowNodeApprover> delApproverWrapper = new LambdaQueryWrapper<>();
            delApproverWrapper.eq(SysFlowNodeApprover::getDefinitionId,id);
            sysFlowNodeApproverMapper.delete(delApproverWrapper);
            //删除节点下所有的事件
            LambdaQueryWrapper<SysFlowNodeEvent> delEventWrapper = new LambdaQueryWrapper<>();
            delEventWrapper.eq(SysFlowNodeEvent::getDefinitionId,id);
            sysFlowNodeEventMapper.delete(delEventWrapper);
            return Result.succ(1,"删除成功","");
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
